// utility.h : main header file for the utility DLL
//

#pragma once

#include <windows.h>

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <tchar.h>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <math.h>
#include <float.h>
#include <limits.h>

#include <time.h>
#include <errno.h>

#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>


using namespace std;


#if defined _UNICODE || defined UNICODE
     typedef wstring tstring;
#else
	typedef string tstring;
#endif

// CutilityApp
// See utility.cpp for the implementation of this class
//

#define gcs ", "

typedef set<string> StringSet;

typedef vector<DWORD> ThreadArray;
typedef vector<HWND>  WindowArray;


typedef struct DataBufferStatus {
	int date;
	int code;
	time_t timestamp;
	unsigned int nCount;
} DataBufferStatus;


class  CriticalLock {
	CRITICAL_SECTION cs;
public:
	CriticalLock() {
		InitializeCriticalSection(&cs);
	}
	~CriticalLock() {
		DeleteCriticalSection(&cs);
	}

	void EnterCritical() {
		EnterCriticalSection(&cs);
	}

	void LeaveCritical() {
		LeaveCriticalSection(&cs);
	}

	class Scoped {
		CriticalLock* _cl;
	public:
		Scoped(CriticalLock& cl) : _cl(&cl) {
			_cl->EnterCritical();
		}

		~Scoped() { _cl->LeaveCritical(); }
	};
};


#define MAX_NAME_LEN  32
#define MAX_TIME_LEN  32
#define MAX_CODE_LEN   8
#define MAX_CODES 40
#define MAX_URI_NUM 4

typedef struct CtpifProfile{
	CtpifProfile() {
		memset(m_strMdUri, 0, sizeof(char*) * MAX_URI_NUM);
		memset(m_strCodes, 0, sizeof(char*) * MAX_CODES);
		memset(m_strTdUri, 0, sizeof(char*) * MAX_URI_NUM);
	}

	~CtpifProfile() {
		for(int i = 0; i < MAX_CODES; i++)
			if (m_strCodes[i]) free(m_strCodes[i]);

		for (int i = 0; i < MAX_URI_NUM; i++)
			if (m_strMdUri[i]) free(m_strMdUri[i]);

		for (int i = 0; i < MAX_URI_NUM; i++)
			if (m_strTdUri[i]) free(m_strTdUri[i]);
	}

	char     m_strLogPath  [MAX_PATH];
	char     m_strMDLogfile[MAX_PATH];
	char     m_strTDLogfile[MAX_PATH];
	char     m_strLogfile  [MAX_PATH];
	char     m_strUsername [MAX_NAME_LEN];
	char     m_strPassword [MAX_NAME_LEN];
	char     m_strBrokerId [MAX_NAME_LEN];
	char*    m_strMdUri    [MAX_URI_NUM];
	char*    m_strTdUri    [MAX_URI_NUM];
	bool     m_bMdUsingUdp;
	bool     m_bTdUsingUdp;
	char*    m_strCodes[MAX_CODES];
	int      m_nCodeNum;
	int      m_nMdUriNum;
	int      m_nTdUriNum;
	int      m_nMaxDataInBuffer;
	int      m_nMaxTradeInMem;
	int      m_nMaxOrderInMem;
	int      m_nMaxInstruments;
} CtpifProfile;



class  TimeTrans {
public:
	static int    get_day_int(time_t thet = 0);
	static time_t get_zeor_of_theday();
	static time_t make_time_in_same_day(const char * day, const char * strt);
	static time_t make_time_in_same_day(time_t zero, const char * strt);
	static time_t make_time_in_same_day(time_t zero, char * strt);

	static char * get_timenow(char * buf, int maxlen);
	static char * get_time_str(char * buf, int maxlen, time_t tt);
	static char * get_time_str(char * buf, int maxlen, double tt);
	static char * get_time64_str(char * buf, int maxlen, __time64_t tt);
	static TCHAR * get_time_tstr(TCHAR *buf, int maxlen, time_t tt);
	static TCHAR * get_time_tstr(TCHAR *buf, int maxlen, double tt);
	static char * get_day_str(char * buf, int maxlen, time_t tt);
	
	static char * get_today_str(char * buf, int maxlen);
	static time_t make_the_time(time_t tt, int hour = 0, int min = 0, int sec = 0);
	static time_t make_the_time(char * day, char * tt);

	static __time64_t make_time64_in_same_day(__time64_t tt, int hour = 0, int min = 0, int sec = 0);
	static time_t make_time_in_same_day(time_t tt, int hour = 0, int min = 0, int sec = 0);
	static time_t make_time_inday(const char * day, int hour = 0, int min = 0, int sec = 0);
	
	static time_t get_minute(time_t the_time);
	static time_t get_minute_32(time_t the_time);
	static time_t get_time_from_int_format_day(int day);

};


#define sig(x) ((x < 0) ? -1 : ((x > 0) ? 1 : 0))


extern  int get_configure(const char * config_filename, CtpifProfile * profile, const char * program_name);
extern  void ShowErrorCode(LPTSTR lpszFunction, LPTSTR buf, int len);
extern  void delete_newline(char * buf);


const unsigned int TRADE_METHOD_NONE       = 0x00;
const unsigned int TRADE_METHOD_SLOW_IN    = 0x01;
const unsigned int TRADE_METHOD_SLOW_OUT   = 0x02;
const unsigned int TRADE_METHOD_QUICK_IN   = 0x04;
const unsigned int TRADE_METHOD_QUICK_OUT  = 0x08;

extern  char * trade_type_string(int type, char * buf, int len);
extern  TCHAR gmsgbuf[MAX_PATH];
extern  char  msgbuf[MAX_PATH];



#ifndef uint32
typedef unsigned int uint32;
#endif //uint32

#ifndef uchar
typedef unsigned char uchar;
#endif //uint32

#pragma warning(disable : 4251)
